<template>
	<view>
		<u-sticky style="top: 0">
			<view
				style="background-color: #fff;padding: 8px;border-top: 1px solid #F3F5F9; border-bottom: 1px solid #F3F5F9;">
				<u-tabs :scrollable='false' :list="tabs" @click="(item) => {currentTab = item}"></u-tabs>
			</view>
		</u-sticky>

		<view class="infoCells" v-if="currentTab.value == 'base'">
			<u-cell-group>
				<u-cell title="咨询类型" :value="consultant.consultationTypeLabel"></u-cell>
				<u-cell title="发起时间" :value="consultant.whenCreated"></u-cell>
				<u-cell title="接入时间" :value="consultant.acceptTime"></u-cell>
				<template v-if="consultant.consultationStatus == 'finish'">
					<u-cell title="结束时间" :value="consultant.closeTime"></u-cell>
				</template>
				<u-cell title="状态" :value="consultationStatusMap[consultant.consultationStatus]"></u-cell>
				<u-cell title="服务律师" :value="consultant.lawyerName"></u-cell>
				<u-cell title="咨询内容" :label="consultant.question"></u-cell>
			</u-cell-group>
		</view>
		<view v-else-if="currentTab.value == 'list'">
			<view :key="item.id" v-for="(item, index) in allHisMessages">
				<messageInfoHis :message="item" :userInfo="allUserInfos[item.sendUserId]">
				</messageInfoHis>
			</view>
		</view>

		<view v-else-if="currentTab.value == 'satisfactionInfo'">
			<template v-if="consultant.satisfactionEvaluated">
				<satisfactionInfo :satisfactionLog='satisfactionLog' />
			</template>
			<u-empty v-else text='暂无评价'></u-empty>
		</view>
	</view>
</template>

<script>
	import satisfactionInfo from './satisfactionInfo.vue'
	import messageInfoHis from './messageInfoHis.vue'
	import {
		toast,
		clearStorageSync,
		getStorageSync,
		useRouter
	} from '@/utils/utils.js'
	export default {
		components: {
			messageInfoHis,
			satisfactionInfo
		},
		props: {
			consultant: {
				require: true
			}
		},
		watch: {
			consultant: {
				immediate: true,
				handler: function(val) {
					this.searchHis()
					if (val?.consultationStatus == 'finish') {
						this.getSatisfactionLog()
					}
				}
			}
		},
		data() {
			return {
				currentTab: {
					name: '基础信息',
					value: 'base'
				},
				consultationStatusMap: {
					wait: '等待律师接入',
					in_service: '服务中',
					finish: '服务完成',
					termination: '服务中止'
				},
				satisfactionOptions: [],
				satisfactionResultMap: {},
				subOptsMap: {
					// service: '服务态度',
					// efficiency: '效率',
					// environment: '环境',
					// ability: '能力',
					// honest: '廉洁',
					// other: '其它'
				},
				subOpts: [],
				allHisMessages: [],
				allUserInfos: {},
				satisfactionLog: {}
			}
		},
		computed: {
			tabs: function() {
				let base = [{
					name: '基础信息',
					value: 'base'
				}, {
					name: '咨询记录',
					value: 'list'
				}]
				if (this.consultant.consultationStatus == 'finish') {
					base.push({
						name: '满意度信息',
						value: 'satisfactionInfo'
					})
				}
				return base
			}
		},
		methods: {
			getSatisfactionLog: function() {
				this.$api.satisfactionLog({
					icType: 'IM',
					consultationId: this.consultant.id
				}).then(res => {
					if (res.success) {
						this.satisfactionLog = res.result || {}
					} else {
						toast(res.message)
					}
				}).catch(err => {
					console.log('searchHis error', err)
					toast('获取满意度评价失败！')
				})
			},
			searchHis: function() {
				this.$api.getIMHistory({
					groupId: this.consultant.groupId
				}).then(res => {
					if (res.success) {
						this.allHisMessages = res.result?.hisMessages || []
						this.allUserInfos = res.result?.userInfos || {}
					} else {
						toast(res.message)
					}
				}).catch(err => {
					console.log('searchHis error', err)
					toast('获取聊天记录失败！')
				})
			},

		}
	}
</script>

<style>

</style>